package com.qymerge.manage.base.dao.impl;

import com.qymerge.manage.base.dao.IMetaCostCenterDao;
import com.qymerge.manage.base.ro.MetaCostCenterRO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.dao.BaseDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Title: 成本中心(部门)</p>
 * <p>Description: </p>
 *
 * @author Xiao
 * @date 2025/5/12 00:48
 * @version 1.0.0
 */
@Repository
public class MetaCostCenterDaoImpl extends BaseDao implements IMetaCostCenterDao {

    private String dwd_MCRS_MDM_meta_CostCenter = "SELECT Id,Code,Name,ParentId,IsLeaf,Level,Type,Etl_Last_Date FROM dwd_MCRS_MDM_meta_CostCenter WHERE 1=1 ";

    @Override
    public List list(MetaCostCenterRO ro, PageRequest pageRequest) {
        int skip = (pageRequest.getPage() - 1) * pageRequest.getSize();
        int size = pageRequest.getSize();

        String sql = dwd_MCRS_MDM_meta_CostCenter;
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (StringUtils.isNotBlank(ro.getId())) {
            sql += " and Id=:Id";
            paramMap.put("Id", ro.getId());
        }

        if (StringUtils.isNotBlank(ro.getCode())) {
            sql += " and Code like :Code";
            paramMap.put("Code", "%" + ro.getCode() + "%");
        }

        if (StringUtils.isNotBlank(ro.getName())) {
            sql += " and Name like :Name";
            paramMap.put("Name", "%" + ro.getName() + "%");
        }

        if (ro.getType() > -1) {
            sql += " and Type=:Type";
            paramMap.put("Type", ro.getType());
        }

        // 分页数据获取
        sql += " order by Id desc limit :skip,:sizes";
        paramMap.put("skip", skip);
        paramMap.put("sizes", size);

        return getNamedParameterJdbcTemplate().query(sql, paramMap, new BeanPropertyRowMapper<MetaCostCenterRO>(MetaCostCenterRO.class));
    }

    @Override
    public int total(MetaCostCenterRO ro) {
        String sql = "select count(1) from dwd_MCRS_MDM_meta_CostCenter where 1=1";
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (StringUtils.isNotBlank(ro.getId())) {
            sql += " and Id=:Id";
            paramMap.put("Id", ro.getId());
        }

        if (StringUtils.isNotBlank(ro.getCode())) {
            sql += " and Code like :Code";
            paramMap.put("Code", "%" + ro.getCode() + "%");
        }

        if (StringUtils.isNotBlank(ro.getName())) {
            sql += " and Name like :Name";
            paramMap.put("Name", "%" + ro.getName() + "%");
        }

        if (ro.getType() > -1) {
            sql += " and Type=:Type";
            paramMap.put("Type", ro.getType());
        }
        return getNamedParameterJdbcTemplate().queryForObject(sql, paramMap, Integer.class);
    }

    @Override
    public MetaCostCenterRO findOne(String id) {
        StringBuffer sql = new StringBuffer(dwd_MCRS_MDM_meta_CostCenter);
        sql.append(" and Id=?");
        RowMapper<MetaCostCenterRO> rowMapper = new BeanPropertyRowMapper<MetaCostCenterRO>(MetaCostCenterRO.class);
        try {
            return getJdbcTemplate().queryForObject(sql.toString(), rowMapper, id);
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public List<MetaCostCenterRO> findAll(MetaCostCenterRO ro) {
        StringBuffer sql = new StringBuffer(dwd_MCRS_MDM_meta_CostCenter);
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (StringUtils.isNotBlank(ro.getId())) {
            sql.append(" and Id=:Id");
            paramMap.put("Id", ro.getId());
        }

        if (StringUtils.isNotBlank(ro.getCode())) {
            sql.append(" and Code like :Code");
            paramMap.put("Code", "%" + ro.getCode() + "%");
        }

        if (StringUtils.isNotBlank(ro.getName())) {
            sql.append(" and Name like :Name");
            paramMap.put("Name", "%" + ro.getName() + "%");
        }

        if (ro.getType() > -1) {
            sql.append(" and Type=:Type");
            paramMap.put("Type", ro.getType());
        }

        sql.append(" order by Id desc");
        return getNamedParameterJdbcTemplate().query(sql.toString(), paramMap, new BeanPropertyRowMapper<MetaCostCenterRO>(MetaCostCenterRO.class));
    }

    @Override
    public MetaCostCenterRO findOneByCode(String costCenterCode) {
        StringBuffer sql = new StringBuffer(dwd_MCRS_MDM_meta_CostCenter);
        sql.append(" and Code=?");
        RowMapper<MetaCostCenterRO> rowMapper = new BeanPropertyRowMapper<MetaCostCenterRO>(MetaCostCenterRO.class);
        try {
            return getJdbcTemplate().queryForObject(sql.toString(), rowMapper, costCenterCode);
        } catch (DataAccessException e) {
            return null;
        }
    }

}
